package com.mystudy.leetcode.contest.p_5147;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author 周志龙
 * @Email 15216876513@163.com
 * @Date on 2019/8/4 10:44
 * @Description 递减元素使数组呈锯齿状
 */
public class Solution {
    private static final Logger LOGGER = LoggerFactory.getLogger(Solution.class);

    @Test
    public void test() {
        int[] arr = new int[]{9, 6, 1, 6, 2};
        int i = movesToMakeZigzag(arr);
        LOGGER.debug("i = [{}]",i);
    }

    public int movesToMakeZigzag(int[] nums) {
        int count1 = 0;
        int count2 = 0;
        int[] nums1 = new int[nums.length];
        for (int j = 0; j < nums.length; j++) {
            nums1[j] = nums[j];
        }
        for (int i = 0; i < nums.length; i += 2) {
            if (i % 2 == 0) {
                if (i - 1 > 0) {
                    while (nums[i] <= nums[i - 1]) {
                        nums[i - 1]--;
                        count1++;
                    }
                }
                if (i + 1 < nums.length) {
                    while (nums[i] <= nums[i + 1]) {
                        nums[i + 1]--;
                        count1++;
                    }
                }
            }
        }
        LOGGER.debug("count1 = [{}]",count1);
        for (int i = 1; i < nums1.length; i += 2) {
            if (i % 2 != 0) {
                while (nums1[i] <= nums1[i - 1]) {
                    nums1[i - 1]--;
                    count2++;
                }
                if (i + 1 < nums1.length) {
                    while (nums1[i] <= nums1[i + 1]) {
                        nums1[i + 1]--;
                        count2++;
                    }
                }
            }
        }
        LOGGER.debug("count2 = [{}]",count2);
        return count1 > count2 ? count2 : count1;

    }
}
